package com.yylending.las.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tbruyelle.rxpermissions.RxPermissions;
import com.umeng.analytics.MobclickAgent;
import com.yyfax.common.toast.CompatToast;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IEvent;
import com.yyfax.infrastructure.mvp.ViewActivity;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.Constants;
import com.yylending.las.common.widgets.CommonDialog;
import com.yylending.las.common.widgets.LoadingProgressDialog;
import com.yylending.las.main.MainActivity;
import com.yylending.las.tinker.util.Utils;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class BaseActivity extends ViewActivity implements View.OnClickListener {
	private final static String TAG = "BaseActivity";

	private final static String KEY_NEED_GESTURE = "need_gesture";

	FrameLayout mContentView;
	RelativeLayout mTopBar;
	ImageView mBtnBack;
	TextView mTitleCenter;
	protected TextView mRightTx;
	protected TextView mTitleLeft;
	private ImageView mRightIv;

	private CommonDialog mDialog;
	protected RxPermissions mRxPermissions;

	private BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if(TextUtils.equals(Constants.KEY_INTENT_FINISH_ALL_APP, intent.getAction())) {
				finish();
			}
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(getClass().getSimpleName());
		MobclickAgent.onResume(this);
		Utils.setBackground(false);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(getClass().getSimpleName());
		MobclickAgent.onPause(this);
		Utils.setBackground(true);
	}

	@Override
	protected void onDestroy() {
		EventAgent.unregister(this);
		unregisterReceiver(mFinishReceiver);
		dismissCommonDialog();
		mDialog = null;
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_base_title_bar);
		EventAgent.register(this);
		initBaseView();
		mRxPermissions = new RxPermissions(this);

		IntentFilter filter = new IntentFilter();
		filter.addAction(Constants.KEY_INTENT_FINISH_ALL_APP);
		registerReceiver(mFinishReceiver, filter);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		restoreData(savedInstanceState);
	}

	private void restoreData(Bundle savedInstanceState) {
	}

	private void initBaseView() {
		mContentView = (FrameLayout) findViewById(R.id.content_layout_container);
		mTopBar = (RelativeLayout) findViewById(R.id.layout_top_bar);
		//mTopBar.setBackgroundColor(getTopBarBackgroundColor());
		mBtnBack = (ImageView) findViewById(R.id.btn_back);
		mTitleCenter = (TextView) findViewById(R.id.title_center);
		mRightTx = (TextView) findViewById(R.id.btn_right_text);
		mTitleLeft = (TextView) findViewById(R.id.title_left);
		mRightIv = (ImageView) findViewById(R.id.btn_right_image);
		mBtnBack.setOnClickListener(this);
		mRightTx.setOnClickListener(this);
		mRightIv.setOnClickListener(this);
	}

	@Override
	public void setContentView(int layoutId) {
		View view = LayoutInflater.from(this).inflate(layoutId, null);
		setContentView(view);
	}

	@Override
	public void setContentView(View view) {
		if (mContentView.getChildCount() > 1) {
			mContentView.removeViewAt(1);
		}
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
		mContentView.addView(view, lp);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_back:
				onBackBtnPressed();
				break;
			case R.id.btn_right_text:
				onRightBtnPressed();
				break;
			case R.id.btn_right_image:
				onRightImgPressed();
				break;
		}
	}

	protected void onBackBtnPressed() {
		finish();
	}

	public void onRightBtnPressed() {
	}

	public void onRightImgPressed() {
	}

	public void setTopBarVisible(boolean flag) {
		mTopBar.setVisibility(flag ? View.VISIBLE : View.GONE);
	}

	public void setTitle(String title) {
		mTitleCenter.setText(title);
	}

	public void setTitleColorId(int colorId) {
		mTitleCenter.setTextColor(colorId);
	}

	public void setTitle(int id) {
		mTitleCenter.setText(id);
	}

	public void setTitleVisible(boolean flag) {
		mTitleCenter.setVisibility(flag ? View.VISIBLE : View.GONE);
	}

	public void setBtnBackVisible(boolean flag) {
		mBtnBack.setVisibility(flag ? View.VISIBLE : View.GONE);
	}

	public void setRightTx(int id) {
		mRightTx.setText(id);
	}

	public void setRightTx(String rightText) {
		mRightTx.setText(rightText);
	}

	public void setRightTextVisible(boolean flag) {
		mRightTx.setVisibility(flag ? View.VISIBLE : View.GONE);
	}

	public void setLeftTitleVisible(boolean flag) {
		mTitleLeft.setVisibility(flag ? View.VISIBLE : View.GONE);
	}

	public void showRightImg(int drawableId) {
		mRightIv.setImageResource(drawableId);
		mRightIv.setVisibility(View.VISIBLE);
	}

	public void hideRightImg(int drawableId) {
		mRightIv.setVisibility(View.GONE);
	}

	public void setBtnBackEnable(boolean flag) {
		mBtnBack.setEnabled(flag);
	}

	public int getTopBarBackgroundColor() {
		return ContextCompat.getColor(this, R.color.color_33a3ff);
	}

	/**
	 * 会话超时处理
	 *
	 * @param event 会话超时event
	 */
	@Subscribe(sticky = true, threadMode = ThreadMode.MAIN, priority = 1)
	public void handleSessionTimeout(IEvent.UnLoginEvent event) {
		EventAgent.removeStickyEvent(event);
		// 这里再判断一下，如果当前标记是登录状态才往下传，否则说明已经是未登录状态了，不需要再跳转，防止多次跳转。
		if (YJKD.login()) {
			// 会话超时，需要清除本地会话缓存
			YJKD.logout();
			logoutToHomePage();
		}
	}

	/**
	 * 会话超时，或退出登录界面时，则跳转到首页，
	 * 解决修改密码，修改手机号后退栈回到的界面依然是已登录状态的问题。
	 */
	protected void logoutToHomePage() {
		if (!YJKD.login()) {
			MainActivity.start(this, true);
		}
	}

	protected void showToast(int id) {
		CompatToast.showShortToast(getApplicationContext(), getString(id));
	}

	protected void showToast(String text) {
		CompatToast.showShortToast(getApplicationContext(), text);
	}

	protected void showLongToast(String text) {
		CompatToast.showLongToast(getApplicationContext(), text);
	}

	protected void showLongToast(int id) {
		CompatToast.showLongToast(getApplicationContext(), getString(id));
	}

	private void dismissCommonDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
		}
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param title         提示标题
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 * @param cancelString  取消按钮
	 * @param listener      按钮事件监听
	 * @param cancelAble
	 */
	public void showTwoBtnDialog(String title, String content, String confirmString, String cancelString,
			CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		dismissCommonDialog();
		mDialog = new CommonDialog(this, title, content, confirmString, cancelString, listener, cancelAble);
		mDialog.show();
	}

	public void showTwoBtnDialogWithLeftTextColor(String title, String content, String confirmString, String cancelString,
			CommonDialog.ButtonClickListener listener, boolean cancelAble, int leftTextColorId) {
		dismissCommonDialog();
		mDialog = new CommonDialog(this, title, content, confirmString, cancelString, listener, cancelAble);
		mDialog.setLeftBtnTextColor(leftTextColorId);
		mDialog.show();
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param title         提示标题
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 * @param cancelString  取消按钮
	 * @param listener      按钮事件监听
	 * @param cancelAble
	 */
	public void showTwoBtnDialog(String title, String content, String confirmString, String cancelString,
			CommonDialog.ButtonClickListener listener, boolean cancelAble, int layoutId) {
		dismissCommonDialog();
		mDialog = new CommonDialog(this, title, content, confirmString, cancelString, listener, cancelAble, layoutId);
		mDialog.show();
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param title         提示标题
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 * @param cancelString  取消按钮
	 */
	public void showTwoBtnDialog(String title, String content, String confirmString, String cancelString) {
		showTwoBtnDialog(title, content, confirmString, cancelString, null, true);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 * @param cancelString  取消按钮
	 */
	public void showTwoBtnDialog(String content, String confirmString, String cancelString) {
		showTwoBtnDialog(null, content, confirmString, cancelString, null, true);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param titleId    提示标题字符串id
	 * @param contentId  提示内容字符串id
	 * @param confirmId  确认按钮字符串id
	 * @param cancelId   取消按钮字符串id
	 * @param listener   按钮事件监听
	 * @param cancelAble
	 */
	public void showTwoBtnDialog(int titleId, int contentId, int confirmId, int cancelId,
			CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(getString(titleId), getString(contentId), getString(confirmId), getString(cancelId), listener, cancelAble);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param contentId  提示内容字符串id
	 * @param confirmId  确认按钮字符串id
	 * @param cancelId   取消按钮字符串id
	 * @param listener   按钮事件监听
	 * @param cancelAble
	 */
	public void showTwoBtnDialog(int contentId, int confirmId, int cancelId,
			CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(null, getString(contentId), getString(confirmId), getString(cancelId), listener, cancelAble);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param content 提示内容字符串
	 * @param confirm  确认按钮字符串
	 * @param cancel   取消按钮字符串
	 * @param listener   按钮事件监听
	 * @param cancelAble
	 */
	public void showTwoBtnDialog(String content, String confirm, String cancel,
			CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(null, content, confirm, cancel, listener, cancelAble);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param titleId   提示标题字符串id
	 * @param contentId 提示内容字符串id
	 * @param confirmId 确认按钮字符串id
	 * @param cancelId  取消按钮字符串id
	 */
	public void showTwoBtnDialog(int titleId, int contentId, int confirmId, int cancelId) {
		showTwoBtnDialog(getString(titleId), getString(contentId), getString(confirmId), getString(cancelId), null, true);
	}

	/**
	 * 弹出两个按钮的提示框
	 *
	 * @param contentId 提示内容字符串id
	 * @param confirmId 确认按钮字符串id
	 * @param cancelId  取消按钮字符串id
	 */
	public void showTwoBtnDialog(int contentId, int confirmId, int cancelId) {
		showTwoBtnDialog(null, getString(contentId), getString(confirmId), getString(cancelId), null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param title         提示标题
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 * @param listener      按钮事件监听
	 * @param cancelAble
	 */
	public void showOneBtnDialog(String title, String content, String confirmString, CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(title, content, confirmString, "", listener, cancelAble);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param titleId    提示标题字符串id
	 * @param contentId  提示内容字符串id
	 * @param confirmId  确认按钮字符串id
	 * @param listener   按钮事件监听
	 * @param cancelAble
	 */
	public void showOneBtnDialog(int titleId, int contentId, int confirmId, CommonDialog.ButtonClickListener listener, boolean cancelAble) {
		showTwoBtnDialog(getString(titleId), getString(contentId), getString(confirmId), "", listener, cancelAble);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param titleId   提示标题字符串id
	 * @param contentId 提示内容字符串id
	 * @param confirmId 确认按钮字符串id
	 */
	public void showOneBtnDialog(int titleId, int contentId, int confirmId) {
		showTwoBtnDialog(getString(titleId), getString(contentId), getString(confirmId), "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param title         提示标题
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 */
	public void showOneBtnDialog(String title, String content, String confirmString) {
		showTwoBtnDialog(title, content, confirmString, "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 */
	public void showOneBtnDialog(String content, String confirmString) {
		showTwoBtnDialog(null, content, confirmString, "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param content 提示内容
	 */
	public void showOneBtnDialog(String content) {
		showTwoBtnDialog(null, content, getString(R.string.confirm), "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param contentId 提示内容
	 * @param confirmId 确认按钮
	 */
	public void showOneBtnDialog(int contentId, int confirmId) {
		showTwoBtnDialog(null, getString(contentId), getString(confirmId), "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param contentId 提示内容
	 */
	public void showOneBtnDialog(int contentId) {
		showTwoBtnDialog(null, getString(contentId), getString(R.string.confirm), "", null, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 */
	public void showOneBtnDialog(String content, String confirmString, CommonDialog.ButtonClickListener listener) {
		showTwoBtnDialog(null, content, confirmString, "", listener, true);
	}

	/**
	 * 弹出单个按钮的提示框
	 *
	 * @param content       提示内容
	 * @param confirmString 确认按钮
	 */
	public void showOneBtnDialog(String content, String confirmString, CommonDialog.ButtonClickListener listener, int layoutId) {
		showTwoBtnDialog(null, content, confirmString, "", listener, true, layoutId);
	}

	@Override
	protected void initProgressDialog(boolean cancelable) {
		mProgressDialog = new LoadingProgressDialog(this, R.style.loading_progress_dialog, "");
		mProgressDialog.setCanceledOnTouchOutside(cancelable);
		mProgressDialog.setCancelable(cancelable);
		mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				mProgressDialog.dismiss();
			}
		});
	}
}